<?
ob_start(); 
include("common/header.html");
include('common/autoload.php');
$out_html = ob_get_contents();
ob_end_clean();
include_once("class/ProOrder_dt.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = SoapInit::GetSoapInside();
$memberID=$_SESSION['sid'];

// 异步获取对应的景点门票
if($_SERVER['HTTP_REQUEST_TYPE']=='getTicket'){
	$sql='SELECT id,title FROM uu_jq_ticket WHERE landid='.($_REQUEST['landid']+0);//
	$GLOBALS['le']->query($sql);
	$tickes = array();
	while($row=$GLOBALS['le']->fetch_assoc()){
		$tickes[]=$row;
	}
	if(count($tickes)){
		echo json_encode(array('s'=>'success','content'=>$tickes));
	}else{
		echo json_encode(array('s'=>'fail'));
	}
	exit;
}

$btime=$_REQUEST['begintime']?$_REQUEST['begintime']:date("Y-m-d");
$etime=$_REQUEST['endtime']?$_REQUEST['endtime']:date("Y-m-d");
$by=$_REQUEST['by']?$_REQUEST['by']:"lid";
$option=array();
if(count($_REQUEST['status'])>0){
	$option['status']=implode("|",$_REQUEST['status']);
}

// 景点查询
if(isset($_REQUEST['landid']))	$option['lid']=$_REQUEST['landid'];

// 门票查询
if(isset($_REQUEST['ticket'])){
	$t = explode("_",$_REQUEST['ticket']);$option['tid']=$t[0];
}

//交易时间类型
$timeType = isset($_REQUEST['time_type'])? abs($_REQUEST['time_type']) : 1;
//分销商ID
$fid = isset($_REQUEST['fid']) ? abs($_REQUEST['fid']) : 0;
//TODO::直销
$obj = saleProOrderObject($memberID,$s,$_SESSION['sdtype']);
//TODO::分销
//$objTrans =saleProOrderObject($memberID,$s, 'TRANS');
if($_SESSION['dtype']==7){
    //7集团帐号
	$r = $obj->orderSum($btime." 00:00:00",$etime." 23:59:59",$by,
        $option, $timeType, $fid);

//	$r2 = $objTrans->orderSum($btime." 00:00:00",$etime." 23:59:59",$by,
//        $option, $timeType, $fid);
}
else{
	$r = $obj->_orderSum($btime." 00:00:00",$etime." 23:59:59",$by,
        $option, $timeType, $fid);

//    $r2 = $objTrans->_orderSum($btime." 00:00:00",$etime." 23:59:59",$by,
//        $option, $timeType, $fid);
}
//$r = array();
//if (is_array($r1) && is_array($r2)) {
//
// $keysIntersect =  array_intersect_key($r1, $r2);
//    if ( count($keysIntersect) ) {
//        //TODO::取出两个数组中相同的键
//        $keys =  array_keys($keysIntersect);
//        foreach ($keys as $key) {
//            $r[$key] = array(
//                'title'     => $r1[$key]['title'],
//                'order'     => $r1[$key]['order']  + $r2[$key]['order'],
//                'tnum'      => $r1[$key]['tnum']   + $r2[$key]['tnum'],
//                'money'     => $r1[$key]['money']  + $r2[$key]['money'],
//                'pmode0'    => $r1[$key]['pmode0'] + $r2[$key]['pmode0'],
//                'pmode2'    => $r1[$key]['pmode2'] + $r2[$key]['pmode2'],
//                'pmode3'    => $r1[$key]['pmode3'] + $r2[$key]['pmode3'],
//                'pmode1'    => $r1[$key]['pmode1'] + $r2[$key]['pmode1'],
//            );
//            unset($r1[$key]);
//            unset($r2[$key]);
//        }
//    }
//    #print_r($r1);
//    #print_r($r2);
//    //TODO::合并剩余的数组
//    #$r = array_merge($r, $r1, $r2);
//    $r = $r + $r1 + $r2;
//    #print_r($r);exit;
//}
//elseif (is_array($r1)) {
//    $r = $r1;
//}
//elseif (is_array($r2)) {
//    $r = $r2;
//}

if ($_GET['testcgp']==1) {
    print_r($r);
//    $obj2 = saleProOrderObject($memberID,$s,'TRANS');
//    $r2 = $obj2->orderSum("2014-04-01 00:00:00","2014-09-16 23:59:59",$by,$option, $timeType, $fid);
//    $r2 = $obj2->orderSum($btime." 00:00:00",$etime." 23:59:59",$by,$option, $timeType, $fid);
//    print_r($r);
//    print_r(array_merge($r,$r2));
//    $products = $obj2->getOrderProducts(); // 订单中的产品列表
//    print_r($products);
    exit;
}

//供应商身份获取他的分销商
if($_SESSION['dtype']==0 || $_SESSION['dtype']==6 || $_SESSION['dtype']==7) {
	include 'getDistors.php';
	$memberID = ($_SESSION['dtype']==6 || $_SESSION['dtype']==7) ? $_SESSION['sid']:$_SESSION['memberID'];
    if($_SESSION['dtype']==7){
		$DistorsNames = getAll($GLOBALS['le'],$memberID);
	}else{
		$DistorsNames = getRetail($GLOBALS['le'],$memberID);
	}
    //TODO::加载会员数据 ---By Cgp
    $memberNames = require dirname(__FILE__) . '/module/link_product/Member.cache.php';
	$dTypeName =  '分销商';
}

//管理员
if($_SESSION['memberID']==1 && $_SESSION['dtype']==9){
	$aDistorsNames=array();
	$sql='SELECT id,dname FROM pft_member';
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$aDistorsNames[$row['id']]=$row['dname'];
	}
}

if($_SESSION['dtype']!=7){
	if($_REQUEST['by']=='mid'){
		foreach($r as $k => $v){
            if($k==0) {
                $r[$k]['title'] = '<span style="color:red;">'.$memberNames[$_SESSION['memberID']].'(自供自销)</span>';
            }
			else if($_SESSION['memberID']==1 && $_SESSION['dtype']==9){
                $r[$k]['title']=$aDistorsNames[$k];
			}else{
                $r[$k]['title']=$memberNames[$k];
			}
		}
	}
}

$titleT=array("lid"=>"景区名称","tid"=>"票类名称","mid"=>"分销商名称");
$totals=array();
/*导出EXCEL处理程序--begin-*/
include("ExcelHandle.php");
/*导出EXCEL处理程序--end*/
echo $out_html;
$from="sale";


// 获取产品
$products = $obj->getOrderProducts(); // 订单中的产品列表
if($_REQUEST['proList']){ //以有产品列表比查询出来的少更新
	$tmp = unserialize($_REQUEST['proList']);
	if(count($products)<=count($tmp)){
		$products = $tmp;
		$proList = serialize($tmp); 
	}else{
		$proList = serialize($products);
	}
}else{
	$proList = serialize($products);
}

// $sql='SELECT id,title FROM uu_land WHERE apply_did='.$_SESSION['memberID'];//
// $GLOBALS['le']->query($sql);
// $products = array();
// while($row=$GLOBALS['le']->fetch_assoc()){
	// $products[]=$row;
// }
include("tpl/orderReport.tpl");
include("common/footer.html");
?>